In [26]:
import xarray as xr
import numpy as np
import pysd.utils as utils
import textwrap
In [103]:
coords={'Dim2': ['D', 'E'], 'Dim1': ['A', 'B', 'C']}
dims = ['Dim1', 'Dim2']
a = xr.DataArray(data=[[.1, .2],[.3,.4],[.5,.6]], coords=coords, dims=dims)
a
Out[103]:
In [107]:
a = xr.DataArray(data=[3, 4, 5], coords={'Dim1': ['A', 'B', 'C']})
print a, '\n'
b = xr.DataArray(data=a, coords={'Dim1': ['A', 'B', 'C']})
print b
In [108]:
a = xr.DataArray(data=[3, 4, 5], coords={'Dim1': ['A', 'B', 'C']})
print a, '\n'
b = xr.DataArray(data=a, coords={'Dim1': ['D', 'E', 'F']})
print b
In [104]:
a = xr.DataArray(data=[3, 4, 5], coords={'Dim1': ['A', 'B', 'C']})
print a
b = xr.DataArray(data=a, coords={'Dim2': ['D', 'E'], 'Dim1': ['A', 'B', 'C']}, dims=['Dim1', 'Dim2'])
print b
In [109]:
a = xr.DataArray(data=3, coords={'Dim1': ['A', 'B', 'C']})
In [40]:
subs = ['One Dimensional Subscript', 'Second Dimension Subscript', 'Depth 1']
subscript_dict = {'Second Dimension Subscript': ['Column 1', 'Column 2'],
'Third Dimension Subscript': ['Depth 1', 'Depth 2'],
'One Dimensional Subscript': ['Entry 1', 'Entry 2', 'Entry 3']}
coords = utils.make_coord_dict(subs, subscript_dict, terse=False)
dims = [utils.find_subscript_name(subscript_dict, sub) for sub in subs]
shape = [len(coords[dim]) for dim in dims]
text = '1,2; 3,4; 5,6'
text = text.strip(';').replace(' ', '').replace(';',',')
data = np.array([float(s) for s in text.split(',')]).reshape(shape)
datastr = np.array2string(data, separator=',').replace('\n','').replace(' ','')
datastr
a = xr.DataArray(data=data, coords=coords, dims=dims)
a
string = textwrap.dedent("""\
xr.DataArray(data=%(datastr)s,
coords=%(coords)s,
dims=%(dims)s )""" % {
'datastr': datastr,
'coords': coords,
'dims': repr(dims)
})
eval(string)
Out[40]:
In [114]:
# modify values directly without index slicing
coords={'Dim2': ['D', 'E'], 'Dim1': ['A', 'B', 'C']}
dims = ['Dim1', 'Dim2']
shape = [len(coords[key]) for key in dims]
val = [3, 2, 1]
a = xr.DataArray(data=np.tile(val, shape), coords=coords, dims=dims)
a
In [5]:
# using np.zeros
coords={'Dim2': ['D', 'E'], 'Dim1': ['A', 'B', 'C']}
dims = ['Dim1', 'Dim2']
shape = [len(coords[key]) for key in dims]
a = xr.DataArray(data=np.zeros(shape)*np.NaN, coords=coords, dims=dims)
a.loc[coords]
Out[5]:
In [53]:
# using np.tile
coords={'Dim2': ['D', 'E'], 'Dim1': ['A', 'B', 'C']}
dims = ['Dim1', 'Dim2']
shape = [len(coords[key]) for key in dims]
a = xr.DataArray(data=np.tile(np.NaN, shape), coords=coords, dims=dims)
a.loc[coords]
Out[53]:
In [ ]:
In [74]:
# assign all values to a scalar
coords={'Dim2': ['D', 'E'], 'Dim1': ['A', 'B', 'C']}
dims = ['Dim1', 'Dim2']
shape = [len(coords[key]) for key in dims]
a = xr.DataArray(data=np.tile(np.NaN, shape), coords=coords, dims=dims)
a.loc[coords] = 3
a.loc[coords]
Out[74]:
In [75]:
# fill all values
coords={'Dim2': ['D', 'E'], 'Dim1': ['A', 'B', 'C']}
dims = ['Dim1', 'Dim2']
shape = [len(coords[key]) for key in dims]
a = xr.DataArray(data=np.tile(np.NaN, shape), coords=coords, dims=dims)
a.loc[coords] = [1,2],[3,4],[5,6]
a.loc[coords]
Out[75]:
In [76]:
# modify values directly without index slicing
coords={'Dim2': ['D', 'E'], 'Dim1': ['A', 'B', 'C']}
dims = ['Dim1', 'Dim2']
shape = [len(coords[key]) for key in dims]
a = xr.DataArray(data=np.tile(np.NaN, shape), coords=coords, dims=dims)
values = [10,20],[30,40],[50,60]
a.values = np.array(values)
a.loc[coords]
Out[76]:
In [ ]:
# modify values directly without index slicing
coords={'Dim2': ['D', 'E'], 'Dim1': ['A', 'B', 'C']}
dims = ['Dim1', 'Dim2']
shape = [len(coords[key]) for key in dims]
a = xr.DataArray(data=np.tile(np.NaN, shape), coords=coords, dims=dims)
a.loc[]
a.values = np.array(values)
a.loc[coords]
In [9]:
# select a slice
a.loc[{'Dim2':['D']}]
Out[9]:
In [78]:
# assign to a slice (the orientation matters!)
coords={'Dim2': ['D', 'E'], 'Dim1': ['A', 'B', 'C']}
dims = ['Dim1', 'Dim2']
shape = [len(coords[key]) for key in dims]
a = xr.DataArray(data=np.tile(np.NaN, shape), coords=coords, dims=dims)
a.loc[{'Dim2':['D']}] = 1, 2, 3
a.loc[coords]
In [79]:
#assign to a slice with properly aligned array
coords={'Dim2': ['D', 'E'], 'Dim1': ['A', 'B', 'C']}
dims = ['Dim1', 'Dim2']
shape = [len(coords[key]) for key in dims]
a = xr.DataArray(data=np.tile(np.NaN, shape), coords=coords, dims=dims)
a.loc[{'Dim2':['D']}] = [1], [2], [3]
a.loc[coords]
Out[79]:
In [82]:
# assign to opposite dimension
ret = xr.DataArray(data=np.ones([3, 2])*np.NaN,
coords={'Dim2': ['D', 'E'], 'Dim1': ['A', 'B', 'C']},
dims=['Dim1', 'Dim2'] )
ret.loc[{'Dim1': ['A']}] = 50, 51
ret.loc[{'Dim1': ['B']}] = 21, 22
ret.loc[{'Dim1': ['C']}] = 3, 4
ret
Out[82]:
In [87]:
# add a new slice
coords={'Dim2': ['D', 'E'], 'Dim1': ['A', 'B', 'C']}
dims = ['Dim1', 'Dim2']
shape = [len(coords[key]) for key in dims]
a = xr.DataArray(data=np.tile(np.NaN, shape), coords=coords, dims=dims)
new_coords={'Dim1': ['A', 'B', 'C'], 'Dim2': ['D']}
new_slice = xr.DataArray(data=[[1.1, 2.2, 3.3]], coords=new_coords)
a.update(new_slice)
a
In [84]:
a.loc[new_coords] = new_slice
In [85]:
a.loc[new_coords].values = new_slice.loc[new_coords].values
In [93]:
# multiplication works, why not assignment?
coords={'Dim2': ['D', 'E'], 'Dim1': ['A', 'B', 'C']}
dims = ['Dim1', 'Dim2']
a = xr.DataArray(data=[[.1, .2],[.3,.4],[.5,.6]], coords=coords, dims=dims)
new_coords={'Dim1': ['A', 'B', 'C'], 'Dim2': ['D']}
new_slice = xr.DataArray(data=[[1.1, 2.2, 3.3]], coords=new_coords)
a.loc[new_coords] * new_slice.loc[new_coords]
a * new_slice
a + new_slice
Out[93]:
In [28]:
#a2, new_slice2 = xr.align(a,new_slice)
a.loc[new_coords]
Out[28]:
In [100]:
coords={'Dim2': ['D', 'E'], 'Dim1': ['A', 'B', 'C']}
dims = ['Dim1', 'Dim2']
shape = [len(coords[key]) for key in dims]
a = xr.DataArray(data=np.tile(np.NaN, shape), coords=coords, dims=dims)
print 'Original Array:\n-----------------\n', a, '\n'
new_coords={'Dim1': ['A', 'B', 'C'], 'Dim2': ['D']}
new_slice = xr.DataArray(data=[[1.1, 2.2, 3.3]], coords=new_coords)
print 'New Slice:\n-----------------\n', new_slice, '\n'
ds = xr.Dataset({'a':a})
ds.update(xr.Dataset({'a':new_slice}))
print 'Updated Array:\n-----------------\n', b['a'], '\n'
In [2]:
m = xr.DataArray(data=1,
coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['D']})
m
In [ ]:
In [18]:
m = xr.DataArray(data=[1.1, 1.2, 1.3],
coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['D']})
n = xr.DataArray(data=[2.1, 2.2, 2.3],
coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['E']})
In [19]:
m = xr.DataArray(data=[[1.1, 1.2, 1.3]],
coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['D']})
n = xr.DataArray(data=[[2.1, 2.2, 2.3]],
coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['E']})
a = xr.concat([m, n], dim='Dim2')
a
Out[19]:
In [24]:
m = xr.DataArray(data=[[[1.1, 1.2, 1.3]]],
coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['D'], 'Dim3':['F']})
n = xr.DataArray(data=[[[2.1, 2.2, 2.3]]],
coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['E'], 'Dim3':['F']})
o = xr.DataArray(data=[[[3.1, 3.2, 3.3]]],
coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['D'], 'Dim3':['G']})
p = xr.DataArray(data=[[[4.1, 4.2, 4.3]]],
coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['E'], 'Dim3':['G']})
a = xr.concat([m,n,o,p], dim=['Dim2', 'Dim3'])
In [107]:
m = xr.DataArray(data=[[[1.1, 1.2, 1.3]]],
coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['D'], 'Dim3':['F']})
n = xr.DataArray(data=[[[2.1, 2.2, 2.3]]],
coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['E'], 'Dim3':['F']})
o = xr.DataArray(data=[[[3.1, 3.2, 3.3]]],
coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['D'], 'Dim3':['G']})
p = xr.DataArray(data=[[[4.1, 4.2, 4.3]]],
coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['E'], 'Dim3':['G']})
a = xr.align(m,n,o,p, join='outer')
xr.concat(a, 'Dim1')
Out[107]:
In [60]:
m = xr.DataArray(data=[[[1.1, 1.2, 1.3]]],
coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['D'], 'Dim3':['F']})
n = xr.DataArray(data=[[[2.1, 2.2, 2.3]]],
coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['E'], 'Dim3':['F']})
o = xr.DataArray(data=[[[3.1, 3.2, 3.3]]],
coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['D'], 'Dim3':['G']})
p = xr.DataArray(data=[[[4.1, 4.2, 4.3]]],
coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['E'], 'Dim3':['G']})
a = xr.align(m,n,o,p, join='outer')
xr.concat(a, dim=['Dim2', 'Dim3'])
In [101]:
m = xr.DataArray(data=[[[1.1, 1.2, 1.3]]],
coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['D'], 'Dim3':['F']})
n = xr.DataArray(data=[[[2.1, 2.2, 2.3]]],
coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['E'], 'Dim3':['F']})
o = xr.DataArray(data=[[[3.1, 3.2, 3.3]]],
coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['D'], 'Dim3':['G']})
p = xr.DataArray(data=[[[4.1, 4.2, 4.3]]],
coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['E'], 'Dim3':['G']})
def xrmerge(das, accept_new=True):
da = das[0]
for new_da in das[1:]:
# Expand both to have same dimensions, padding with NaN
da, new_da = xr.align(da, new_da, join='outer')
# Fill NaNs one way or the other re. accept_new
da = new_da.fillna(da) if accept_new else da.fillna(new_da)
return da
a = xrmerge([m,n,o,p])
a
Out[101]:
In [114]:
a.loc[{'Dim1':'A'}]
Out[114]:
In [115]:
a.loc[{'Dim2':'D'}]
Out[115]:
In [116]:
a.loc[{'Dim3':'F'}]
Out[116]:
In [117]:
data =[[[ 1.1, 1.2, 1.3],
[ 3.1, 3.2, 3.3]],
[[ 2.1, 2.2, 2.3],
[ 4.1, 4.2, 4.3]]]
b = xr.DataArray(data=data,
coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['D', 'E'], 'Dim3':['F', 'G']})
In [126]:
b.values
Out[126]:
In [136]:
for i in b.coords.iteritems():
print i[0], i[1].values
{key:list(value.values) for key, value in b.coords.iteritems()}
Out[136]:
In [ ]:
In [131]:
for i in b.indexes.iteritems():
print i[0], i[1].values
In [82]:
m = xr.DataArray(data=[[[1.1, 1.2, 1.3]]],
coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['D'], 'Dim3':['F']})
n = xr.DataArray(data=[[[2.1, 2.2, 2.3]]],
coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['E'], 'Dim3':['F']})
o = xr.DataArray(data=[[[3.1, 3.2, 3.3]]],
coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['D'], 'Dim3':['G']})
p = xr.DataArray(data=[[[4.1, 4.2, 4.3]]],
coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['E'], 'Dim3':['G']})
ds = xr.Dataset({'m':m,'n':n,'o':o,'p':p})
ds.to_array()
Out[82]:
In [85]:
m = xr.DataArray(data=[[[1.1, 1.2, 1.3]]],
coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['D'], 'Dim3':['F']})
n = xr.DataArray(data=[[[2.1, 2.2, 2.3]]],
coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['E'], 'Dim3':['F']})
o = xr.DataArray(data=[[[3.1, 3.2, 3.3]]],
coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['D'], 'Dim3':['G']})
p = xr.DataArray(data=[[[4.1, 4.2, 4.3]]],
coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['E'], 'Dim3':['G']})
l = [n,o,p]
for j in l:
xr.concat([m, j])
break
In [ ]:
base = xr.DataArray(data=[[[1.1, 1.2, 1.3]]],
coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['D'], 'Dim3':['F']})
n = xr.DataArray(data=[[[2.1, 2.2, 2.3]]],
coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['E'], 'Dim3':['F']})
o = xr.DataArray(data=[[[3.1, 3.2, 3.3]]],
coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['D'], 'Dim3':['G']})
p = xr.DataArray(data=[[[4.1, 4.2, 4.3]]],
coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['E'], 'Dim3':['G']})
In [41]:
import pandas as pd
df = pd.DataFrame(index=range(5), columns=['a','b','c','d'])
df2 = pd.DataFrame(index=range(3), columns=['a'], data=range(3))
df.update(df2)
df
Out[41]:
In [46]:
df.loc[2:4, 'b'] = 5
df
Out[46]:
df.loc[2:4, 'c'] = [5,6,7] df
In [58]:
upstream_funcs = [
lambda: 5
lambda: xr.DataArray(data=5, coords={'Dim1': ['A', 'B', 'C']})
lambda: xr.DataArray(data=5, coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['D']})
lambda: xr.DataArray(data=5, coords={'Dim1': ['A', 'B', 'C'], 'Dim2':['D', 'E']})
]
In [59]:
Out[59]:
In [68]:
a.coords.values()
Out[68]:
In [73]:
{key:list(val.values) for key, val in a.coords.iteritems()}
Out[73]:
In [ ]: